// const formatTime = date => {
//   const year = date.getFullYear()
//   const month = date.getMonth() + 1
//   const day = date.getDate()
//   const hour = date.getHours()
//   const minute = date.getMinutes()
//   const second = date.getSeconds()

//   return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
// }

import Api from './Api';
const app = getApp();

let userInfo = '';
let openid = "";
let loginOk = false; //控制登录
const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

function formatTime(time, format) {
  let temp = '0000000000' + time
  let len = format.length
  return temp.substr(-len)
}

function autoLogin(){
    let that = this;
    wx.login({
        success(code) {
          console.log(code)
          var data = {
            parameter: "getOpenid",
            js_code: code.code,
          }
          Api.get(data).then(res => {
            console.log(res.data);
            app.openid = res.data.openid;
            that.openid = res.data.openid;
            if(res.iszc == "Y"){
                openid = res.data.openid;
                that.loginOk=true
            }
            console.log(openid);
          }, (err) => { } );
        }
      })
}
function getMyUser(){
    let  that = this;
    wx.showModal({
        title: '温馨提示',
        content: '正在请求您的个人信息',
        success(res) {
          if (res.confirm) {
            wx.getUserProfile({
              desc: "获取你的昵称、头像、地区及性别",
              success: res2 => {
                wx.login({
                  success(code) {
                    var data = {
                      parameter: "getOpenid",
                      js_code: code.code,
                    }
                    Api.get(data).then(res => {
                        console.log(res);
                        openid = res.data.openid;
                        that.loginOk=true
                        console.log(loginOk);
                        wx.setStorage({
                            key:"userfo",
                            data:res.data
                        })
                        registerUser(res2.userInfo, res.data.openid);
                    },
                      (err) => { }
                    );
                  }
                })
              },
              fail: res => {
                //拒绝授权
                // that.showErrorModal('您拒绝了请求');
                return;
              }
            })
          } else if (res.cancel) {
            //拒绝授权 showErrorModal是自定义的提示
            // that.showErrorModal('您拒绝了请求');
            return;
          }
        }
    })
}
function registerUser(info, openid){
    console.log(info);
    var that = this;
    var data = {
      parameter: "login",
      openid: openid,
      nickname: escape(info.nickName),
      headimgurl: info.avatarUrl,
      sex: info.gender == 1 ? '1' : '0',
    }
    console.log(data);
    
    Api.get(data).then(res => {
      console.log(res.data);
      if (res.data.code == '1000') {
        wx.showToast({
          title: '登录成功',
          icon:'success',
          duration:1500
        })
      }
    }, (err) => { });
  }
  

  const MyPage = (num = 1, page, pagesize, count) => {
    //num 0 翻页 1 初始调用，重置page，重新搜索 2 page不变 
    if (num == 1) { return { page: 1 ,limit: pagesize} }
    if (num == 2) { return { page: 1 ,limit: page * pagesize } }
    else if (count <= page * pagesize) {return false; }
    else if (num == 0) { return { page: ( (page * 1) + 1) ,limit: pagesize } }
  }

  let qmurl = "";
  const setqmurl = (url) => { qmurl = url; }
  const getqmurl = (url) => { return qmurl }
  let myinfo = "";
  const setmyinfo = (info) => { myinfo = info}
  const getmyinfo = (info) => { return myinfo }
 /*  let gzbh=""
  const setgzbh = (bh) => { gzbh = bh}
  const getgzbh = (bh) => { return gzbh } */


  //获取参数列表
  const getCslb = (lx) => {
    var data = {
        parameter:"selectWxgdData",lx,
    }
    api.post(data).then(res=>{
        return         
    })
  }
/* 防抖函数封装 */
  function debounce(fn, interval) {
    let timer;
    let delay = interval || 1000; // 间隔的时间，如果interval不传，则默认1秒
    return function () {
        let that = this;
        let args = arguments; // 保存此处的arguments，因为setTimeout是全局的，arguments不是防抖函数需要的。
        if (timer) {
            clearTimeout(timer);
        }
        timer = setTimeout(function () {
            fn.apply(that, args); // 用apply指向调用debounce的对象，相当于this.fn(args);
        }, delay);
    };
}
module.exports = {
  formatTime: formatTime,
  autoLogin:autoLogin,
  getMyUser:getMyUser,
  openid: openid,
  MyPage:MyPage,
  userInfo:userInfo,
  setqmurl,getqmurl,
  setmyinfo,getmyinfo,debounce,
  loginOk
}

